
import time

import requests
import json
import logging
import os
import socket
import time
from decimal import Decimal
from pb.comfuc import Comfuc
from pb.sport import Sport
import random
import logging

class GetPBodds:
    session = requests.Session()
    # 创建头信息字典
    headers = {
        "Host": "www.pin135.com",
        "Sec-Ch-Ua-Platform": "Windows",
        "Sec-Ch-Ua": 'Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
        "Sec-Ch-Ua-Mobile": "?0",
        "X-Trust-Client": "false",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
        "Accept": "application/json, text/plain, */*",
        "Origin": "https://www.pin135.com",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://www.pin135.com",
        "Priority": "u=1, i"
    }

    def login(self):
        # URL和查询参数
        url = "https://www.pin135.com/member-service/v2/authenticate"
        params = {
            "locale": "en_US",
            "_": str(int(time.time() * 1000)),
            "withCredentials": "true"
        }


        # 请求体数据
        data = {
            "captcha": "",
            "captchaToken": "",
            "loginId": "pangchaojie",
            "password": "plj3ytovvt25"
        }

        # 发送POST请求
        response = self.session.post(url, params=params, headers=self.headers, data=data)
        jdata = response.json()
        # 打印响应内容
        print(response.status_code)
        print(jdata)
        jdata = response.json()
        self.headers['X-Lcu'] = jdata.get('X-Lcu')
        self.headers['X-U'] = jdata.get('X-U')
        self.headers['X-Slid'] = jdata.get('X-SLID')
        self.headers['X-Custid'] = jdata.get('X-Custid')
        self.headers['X-Browser-Session-Id'] = jdata.get('X-Browser-Session-Id')

    def fetch_match_list(self):
        # 设置 SOCKS5 代理
#https://www.rowilong.com/sports-service/sv/compact/events?btg=1&c=&cl=3&d=&ec=&ev=&g=QQ%3D%3D&hle=false&ic=false&inl=false&l=3&lang=&lg=&lv=&me=0&mk=1&more=false&o=1&ot=1&pa=0&pimo=0%2C1%2C8%2C39%2C2%2C3%2C6%2C7%2C4%2C5&pn=-1&pv=1&sp=29&tm=0&v=1740071613588&locale=zh_CN&_=1740071676977&withCredentials=true
        # URL 和用户名、密码 locale=zh_C 改成 en_US
        url = 'https://www.pin135.com/sports-service/sv/compact/events?btg=1&c=&cl=3&d=&ec=&ev=&g=QQ%3D%3D&hle=false&ic=false&inl=false&l=3&lang=&lg=&lv=&me=0&mk=1&more=false&o=1&ot=1&pa=0&pimo=0%2C1%2C8%2C39%2C2%2C3%2C6%2C7%2C4%2C5&pn=-1&pv=1&sp=29&tm=0&locale=en_US&withCredentials=true' + '&_=' + str(
            int(round(time.time() * 1000)))









        try:

            # 发送 GET 请求
            response = self.session.get(url, headers=self.headers, timeout=10)

            # 输出结果
            # print("Status Code:", response.status_code)
            # 如果返回是 JSON 格式
            if response.status_code == 200:
                self.session.close()
                print(response.json())
                cookies = self.session.cookies
                print("Cookies:", dict(cookies))
                return response.json()
            else:
                logging.error(f"请求失败，状态码：{response.status_code}")
                logging.error("响应内容：", response.text)

        except Exception as e:
            #print("Response Text:", response.text)
            logging.error(f"请求过程中发生错误：{e}")
            time.sleep(6)

    def getList(self):
        sports = []
        list = self.fetch_match_list()
        # print(list['n'][0][2])
        for ls in list['n'][0][2]:
            lsname = ls[1]
            for bs in ls[2]:
                sport = Sport(bs[1], bs[2], lsname)
                sport.sport_id = bs[0]
                sport.start_time = bs[4]
                # print(sport)
                for odd_ou in bs[8].get('0')[1]:
                    sport.full_ou_odd[odd_ou[1]] = [float(odd_ou[2]), float(odd_ou[3])]
                for odd_hdp in bs[8].get('0')[0]:
                    sport.full_hdp_odd[odd_hdp[1]] = [float(odd_hdp[3]), float(odd_hdp[4])]
                if len(sport.full_hdp_odd) == 0:
                    continue
                odds = next(iter(sport.full_hdp_odd.values()))
                return_rate = Comfuc.calculate_return_rate(odds[0], odds[1])
                if sport.start_time / 1000 - time.time() < 3600 and return_rate > 96:
                    sport.return_rate = return_rate
                    sports.append(sport)
        return sports

if __name__ == "__main__":
    getPBodds = GetPBodds()
   # getPBodds.login()
    time.sleep(6)
    sports = []
    list = getPBodds.getList()
    print(len(sports))
    for s in sports:
        print(s)
        print(s.full_ou_odd)
